package com.ss.whisper2.rabbitmq;

import com.ss.whisper2.pojo.entity.course.SelectCourseMessage;
import com.ss.whisper2.rabbitmq.topic.TopicRabbitConfig;
import com.ss.whisper2.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
public class SendMessageController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法


	@GetMapping("/sendDirectMessage")
	public String sendDirectMessage() {
		String messageId = String.valueOf(UUID.randomUUID());
		String messageData = "test message.hello!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String,Object> map=new HashMap<>();
		map.put("messageId",messageId);
		map.put("messageData",messageData);
		map.put("createTime",createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
		return "ok";
	}

	@GetMapping("/sendFanoutMessage")
	public String sendFanoutMessage() {
		String messageId = String.valueOf(UUID.randomUUID());
		String messageData = "message: testFanoutMessage ";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		rabbitTemplate.convertAndSend("fanoutExchange", null, map);
		return "ok";
	}

	//消息推送到server，但是在server里找不到交换机
	//触发的是 ConfirmCallback
	@GetMapping("/TestMessageAck")
	public String TestMessageAck() {
		String messageId = String.valueOf(UUID.randomUUID());
		String messageData = "message: non-existent-exchange test message ";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
		return "ok";
	}


	//消息推送到server，找到交换机了，但是没找到队列
	//触发的是 ConfirmCallback和ReturnCallback
	@GetMapping("/TestMessageAck2")
	public String TestMessageAck2() {
		String messageId = String.valueOf(UUID.randomUUID());
		String messageData = "message: lonelyDirectExchange test message ";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String, Object> map = new HashMap<>();
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
		return "ok";
	}


}


